#include <EEPROM.h>

#define KeyPin  10     //按键开关引脚定义，红色
#define FkeyPin A5     //频率切换按键，白色

#define HunhePin  5     //按键开关引脚定义，红色

#define RANDOMMM 8        //点击随机时间幅度

float hong = 6.8;         //红色按键频率
float bai = 7.6;          //白色按键频率


int minTime0 = int(500/hong) - RANDOMMM;  //最小随机间隔时间
int maxTime0 = int(500/hong) + RANDOMMM;  //最大随机间隔时间

int minTime1 = int(500/bai) - RANDOMMM;  //最小随机间隔时间
int maxTime1 = int(500/bai) + RANDOMMM;  //最大随机间隔时间

//红色按住时间随机最大值和最小值75.4-->1000/75.4=14
#define HONGCOUNT 14
int minTTime_h = 77.8;
int maxTTime_h = 73;

//白色按住时间随机最大值和最小值68--->1000/68=15
#define BAICOUNT 15
int minTTime_b = 70;
int maxTTime_b = 66;

const int tPin[2] = {A2,A1};  //1号引脚定义,灰的在前，白的在后


void touchPin(){
    digitalWrite(tPin[1], HIGH);
    digitalWrite(tPin[0], LOW);  
}
void unTouchPin(){
    digitalWrite(tPin[0], HIGH);
    digitalWrite(tPin[1], LOW);  
}

const int blinkdelay = 300;

void ledBlink(){
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(blinkdelay);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(blinkdelay); 
}



void setup() {
  //初始化串口
  Serial.begin(115200);

  //设置随机数种子
  randomSeed(analogRead(A0));
  pinMode(KeyPin, INPUT); //设置按键引脚为输入
  pinMode(FkeyPin, INPUT); //设置按键引脚为输入
  pinMode(HunhePin,INPUT_PULLUP);

  pinMode(LED_BUILTIN, OUTPUT);
  //设置点击头控制引脚为输出
    pinMode(tPin[0], OUTPUT);
    pinMode(tPin[1], OUTPUT);
  delay(1);
  //初始化点击头状态,清除所有当关点击状态
  unTouchPin();
  digitalWrite(LED_BUILTIN, LOW);

}






int ttime = 125;
int utime = 125;


//======================蓝牙





//======================




int8_t hcount = HONGCOUNT;
int8_t bcount = BAICOUNT;

void loop() {
  int k = digitalRead(HunhePin);
  if(!k){
          if(hcount >= 0){
             //红色
              ttime = random(minTTime_h,maxTTime_h);   //取随机时间
              touchPin();
              delay(ttime);
              utime = random(minTime0,maxTime0);   //取随机时间
              unTouchPin();
              delay(utime);   
              hcount--;
          }else{
              if(bcount >=0){
                    ttime = random(minTTime_b,maxTTime_b);   //取随机时间
                    touchPin();
                    delay(ttime);
                    utime = random(minTime1,maxTime1);   //取随机时间
                    unTouchPin();
                    delay(utime);    
                    bcount--;
              }else{
                hcount = HONGCOUNT;
                bcount = BAICOUNT;
              }
          }
    //白色
          
      }else{
            int k = digitalRead(KeyPin);  
            if(!k){ //红色
                  ttime = random(minTTime_h,maxTTime_h);   //取随机时间
                  touchPin();
                  delay(ttime);
                  utime = random(minTime0,maxTime0);   //取随机时间
                  unTouchPin();
                  delay(utime);
            }else{
                int fk = digitalRead(FkeyPin);
                if(!fk){ //白色
                    ttime = random(minTTime_b,maxTTime_b);   //取随机时间
                    touchPin();
                    delay(ttime);
                    utime = random(minTime1,maxTime1);   //取随机时间
                    unTouchPin();
                    delay(utime);        
                }
            }
      }
}

